//当ajax使用时附上csrf_token
$.ajaxSetup({
   headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}
});


window.onload=function(){
    settingWidth();
    chooseStyle();
}

window.onresize=resizeBannerImage;//当窗口改变宽度时执行此函数
function resizeBannerImage(){
	settingWidth();
}

function settingWidth(){
	var main = document.getElementById('mainMenu');
	var totalWidth = mainMenu.offsetWidth;
	var aLi = mainMenu.getElementsByTagName('li');
    var addListCell = getByClass(main,'add_list_cell');
	var liWidth = (totalWidth-12*14)/6;
	for(var i=0;i<aLi.length;i++){
		aLi[i].style.width = liWidth+'px';
		aLi[i].style.height = liWidth+'px';
	}
}

function addDiv(){
	var clientWidth = document.documentElement.clientWidth;
	var clientHeight = document.documentElement.clientHeight;
	$("#InputName").css("height", clientHeight+"px");
	$(".InputName_bg").css("height", clientHeight+"px");
	$("#InputName").removeClass("dpn");
}

$(function(){
    $(".product_btn").click(function(){
    	var tipName = $("#tipName").val();
    	if(tipName==""){
    	    alert("请先填写名称");
    	}else{
	        $.ajax({
				type:"post",
				url:"products",
				async:false,
				dataType:'json',
				data:{
					name:tipName,
					description:'这是测试产品'
				},
				success:function(data){
					console.log(data.message);
					var i = $(this).attr("count");
//			    	$("div#mainMenu li").eq(i).html(tipName);
				    var str='<li>'+
							    '<div class="add_list_box">'+
							        '<div class="add_list_top">'+
								        '<a class="closeLi" onclick="deleteProduct(this);">'+'</a>'+
							   		    '<img class="filePicture" src="img/filePicture.png">'+
							   		'</div>'+
							   		'<div class="add_list_bottom">'+
							   		     '<a class="add_list_cell">'+tipName+'</a>'+
							   		'</div>'+
							   	'</div>'+
							'</li>';
			    	$('#menuList').append(str);
			    	settingWidth();
			    	var m = parseInt(i)+1;
			        $(this).attr("count", m);
			        $("#InputName").addClass("dpn");

			        chooseStyle();
			        window.location.reload();//刷新界面
				},
				error:function(){
					alert('ajax请求错误~！');
				},
			});
    	}
    });

    $(".InputName_bg").click(function(){
    	$("#InputName").addClass("dpn");
    });
});


function deleteProduct(obj){
	var pid = $(obj).attr("pid");
	console.log(pid);
	if(pid == ""){
		alert("产品参数错误")
	}else{
		$.ajax({
			type:"delete",
			url:"products/"+pid,
			async:false,
			dataType:'json',
			success:function(data){
				$(obj).parent().parent().parent().hide();
			},
			error:function(){
				alert('ajax请求错误');
			}
		});
	}
};


	//class遍历
function getByClass(oParent,sClass){
	var aEle=document.getElementsByTagName('*');//遍历所有对象的类名
	var i=0;
	var aResult=[];

	for(i=0;i<aEle.length;i++){

		if(aEle[i].className==sClass){

			aResult.push(aEle[i]);
		}
	}
	return aResult;
}

//设定样式
function chooseStyle(){
	var main = document.getElementById('mainMenu');
	var addListCell = getByClass(main,'add_list_cell');
	var totalWidth = mainMenu.offsetWidth;
	var liWidth = (totalWidth-16*10)/8;
	for(var i=0;i<addListCell.length;i++){
		var cellWidth = liWidth*0.28;
		addListCell[i].style.height = cellWidth+'px';
		addListCell[i].style.lineHeight = cellWidth+'px';
		addListCell[i].style.overflow = 'hidden';
		addListCell[i].style.whiteSpace = 'nowrap';
		addListCell[i].style.textOverflow = 'ellipsis';
	}
}















































